/*
 * Copyright (c) 2022. China Mobile(SuZhou)Software Technology Co.,Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package service

import (
	"context"
	protobuf2 "github.com/juicedata/juicefs/protobuf"
)

type userFileSystemService struct {}

func NewUserFileSystemService() protobuf2.UserFileSystemServer{
	return &userFileSystemService{}
}

func (userFileSystemService *userFileSystemService) Init(context context.Context,request *protobuf2.InitRequest)(*protobuf2.InitResponse,error){
	res := &protobuf2.InitResponse{}
        if request.Type != primary {
		res.State = 0
	}
	res.State = 1
	return res,nil
}
func (userFileSystemService *userFileSystemService) He3Lseek(context context.Context,request *protobuf2.LseekRequest)( *protobuf2.LseekResponse,error){
        res := &protobuf2.LseekResponse{}
        res.Offset =  Ihe3Lseek(uint64(request.Fd),uint64(request.Offset),uint64(request.Whence))
        return res,nil
}
func (userFileSystemService *userFileSystemService) He3Open(context context.Context,request *protobuf2.OpenRequest)(*protobuf2.OpenResponse, error){
	    var (
	    	fd 	  int
	    	errno int
		)
        res := &protobuf2.OpenResponse{}
        fd,errno = Ihe3Open(request.FileName,request.FileFlages,uint16(request.FileMode),uint8(request.State))
	    res.Fd = int32(fd)
	    res.UErrNo = int32(errno)
        return res,nil
}
func (userFileSystemService *userFileSystemService) WalRead(context context.Context,request *protobuf2.WalReadRequest)(*protobuf2.WalReadResponse,error){
        res := &protobuf2.WalReadResponse{}       
	res.State = int32(IwalRead(request.Fd,request.Buf,request.Offset))
	return res,nil
}
func (userFileSystemService *userFileSystemService) He3Write(context context.Context,request *protobuf2.WriteRequest)(*protobuf2.WriteResponse,error){
	res := &protobuf2.WriteResponse{}
        res.State = int32(Ihe3Write(request.Fd,request.Buf,request.Offset))
	return res,nil
}
func (userFileSystemService *userFileSystemService) WalLocalRead(context context.Context,request *protobuf2.LocalReadRequest)(*protobuf2.LocalReadResponse,error){
	res := &protobuf2.LocalReadResponse{}
        res.State = int32(IwalLocalRead(request.Fd,request.Buf,request.Offset))
	return res,nil
}
func (userFileSystemService *userFileSystemService) WalRestoreRead(context context.Context,request *protobuf2.RestoreReadRequest)(*protobuf2.RestoreReadResponse,error){
        res := &protobuf2.RestoreReadResponse{}
	res.State = int32(IwalRestoreRead(request.Fd,request.Buf,request.Offset))
	return res,nil
}
func (userFileSystemService *userFileSystemService) DataRead(context context.Context,request *protobuf2.DataReadRequest)(*protobuf2.DataReadResponse,error){
        res := &protobuf2.DataReadResponse{}
	res.Data = IdataRead(request.Fd,request.Offset,request.LastLsn)
	return res,nil
}
func (userFileSystemService *userFileSystemService) He3Unlink(context context.Context,request *protobuf2.UnlinkRequest)(*protobuf2.UnlinkResponse,error){
        res := &protobuf2.UnlinkResponse{}
	res.State = int32(Ihe3Unlink(request.FileName))
	return res,nil
}
func (userFileSystemService *userFileSystemService) He3Close(context context.Context,request *protobuf2.CloseRequest)(*protobuf2.CloseResponse,error){

	res := &protobuf2.CloseResponse{}
	res.State = int32(Ihe3Close(request.Fd))
	return res,nil
}

func (userFileSystemService *userFileSystemService) He3Truncate(context context.Context,request *protobuf2.TruncRequest)(*protobuf2.TruncResponse,error){

	res := &protobuf2.TruncResponse{}
	res.State = Ihe3Truncate(request.Fd, request.Offset)
	return res,nil
}

func (userFileSystemService *userFileSystemService) He3Fsync(context context.Context,request *protobuf2.FsyncRequest)(*protobuf2.FsyncResponse,error){

	res := &protobuf2.FsyncResponse{}
	res.State = Ihe3Fsync(request.Fd)
	return res,nil
}